package com.diao.flink.windowAndTimer;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.flink.api.common.functions.FlatMapFunction;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;


/**
 * @author: chenzhidiao
 * @date: 2021/2/2 10:26
 * @description: 通过process方法来自定义处理逻辑，内部需要传一个ProcessWindowFunction实例对象
 * 该对象的process方法就是实际的处理逻辑
 * @version: 1.0
 *
 * 需求：每隔5秒计算最近10秒的单词次数
 *
 * 窗口开始时间：20:17:03
 * 窗口结束时间：20:17:13
 *
 * 窗口开始时间：20:17:08
 * 窗口结束时间：20:17:18
 */
public class WindowWithProcessFunction {
    public static void main(String[] args) throws Exception {
        FastDateFormat dateFormat = FastDateFormat.getInstance("HH:mm:ss");

        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(5);

        DataStreamSource<String> dStream = env.socketTextStream("localhost", 9999);

        SingleOutputStreamOperator<Tuple2<String, Integer>> flatMapedStream = dStream.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split(",");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });


        flatMapedStream.keyBy(tuple -> tuple.f0)
                .window(SlidingProcessingTimeWindows.of(Time.seconds(10),Time.seconds(5)))
                .process(new SumProcessWindowFunction())
                .print().setParallelism(1);

        env.execute("wordCount");
    }


}
/**
 * IN：输入的数据类型
 * OUT：输出的数据类型
 * KEY：Key的数据类型
 * W extends Window：Window的数据类型
 */
class SumProcessWindowFunction extends ProcessWindowFunction<Tuple2<String,Integer>,Tuple2<String,Integer>, String, TimeWindow> {

    FastDateFormat dateformat = FastDateFormat.getInstance("HH:mm:ss");

    @Override
    public void process(String key,  Context context,  Iterable<Tuple2<String, Integer>> elements,  Collector<Tuple2<String, Integer>> out) throws Exception {

        System.out.println("当前系统时间："+dateformat.format(System.currentTimeMillis()));
        System.out.println("窗口处理时间："+dateformat.format(context.currentProcessingTime()));

        System.out.println("窗口开始时间："+dateformat.format(context.window().getStart()));
        System.out.println("窗口结束时间："+dateformat.format(context.window().getEnd()));

        System.out.println("=====================================================");

        int count = 0;
        for(Tuple2<String,Integer> e :elements){
            //统计单词个数
            count++;
        }
        out.collect(Tuple2.of(key,count));

    }
}

